/*  This file is part of -_-.

    -_- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    -_- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with -_-.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2012-2013 Oliver Katz */

#ifndef __OPAL_PARSER_H
#define __OPAL_PARSER_H

#include <iostream>
#include <vector>
#include <sstream>

#include "error.h"
#include "lexer.h"

extern "C"
{
#include "../common/fio.h"
}

using namespace std;

//namespace sleepyface::opal
namespace sleepyface
{
	namespace opal
	{
		string load(string path); //method: read whole file 'path' and return the contents

		//class AST: abstract syntax tree element
		class AST
		{
		private:
			Token root;
			vector<AST> branches;

		public:
			AST(); //methodof AST: initializer
			AST(Token r); //methodof AST: initializer with 'r' as root token
			AST(Token r, vector<AST> b); //methodof AST: initializer with root and branches
			int branch(AST a); //methodof AST: adds a new branch 'a'
			bool isLeaf(); //methodof AST: return true if there are no branches
			Token get(); //methodof AST: gets the root token
			vector<AST> getBranches(); //methodof AST: gets the branch vector
			string display(); //methodof AST: displays the AST
		};

		//class Parser: recursive s-expression parser
		class Parser
		{
		private:
			string leftSExpr;
			string rightSExpr;

		public:
			Parser(); //methodof Parser: initializer with parenthesis
			Parser(string l, string r); //methodof Parser: initializer with left and right bracket tokens
			AST parse(vector<Token> page, string file); //methodof Parser: parses a sequence of tokens into an abstract syntax free
			//argof parse: string file: for error display purposes
		};

		string findImportPath(vector<string> path, string mod, Token dbg, string file); //method: finds the full path of an imported module
		//argof findImportPath: vector<string> path: the possible locations for the module
		//argof findImportPath: string mod: the name of the module
		//argof findImportPath: Token dbg: used for debugging purposes (usually the token of the import command)
		//argof findImportPath: string file: for error display purposes
		AST loadAndParse(string path, Lexer l, Parser p, vector<string> ipath); //method: load a file and parse it
		//argof loadAndParse: string path: the path of the file to load
		//argof loadAndParse: Lexer l: the lexer to use
		//argof loadAndParse: Parser p: the parser to use
		//argof loadAndParse: vector<string> ipath: the import path (like 'path' argument in findImportPath)
		AST handleAllImports(AST ast, string it, Lexer l, Parser p, vector<string> path, string file); //method: hands all the import commands in an AST
		//argof handleAllImports: string it: import command token (usually "import")
	}
}

#endif